package cn.wolfcode.car.business.web.controller;

import cn.wolfcode.car.base.domain.User;
import cn.wolfcode.car.base.service.IRoleService;
import cn.wolfcode.car.base.service.IUserService;
import cn.wolfcode.car.business.domain.BpmnInfo;
import cn.wolfcode.car.business.domain.ServiceItem;
import cn.wolfcode.car.business.query.ServiceItemQuery;
import cn.wolfcode.car.business.service.IBpmnInfoService;
import cn.wolfcode.car.business.service.IServiceItemService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.web.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/business/serviceItem")
public class ServiceItemController {

    private String prefix="business/serviceitem/";
    @Autowired
    private IServiceItemService serviceItemService;
    @Autowired
    private IUserService userService;
    @Autowired
    private IBpmnInfoService bpmnInfoService;

    //tolist
    @RequestMapping("/listPage")
    public String toList(){
        return prefix+"list";
    }
    //toadd.html
    @RequestMapping("/addPage")
    public String toAdd(){
        return prefix+"add";
    }
    //toedit.html
    @RequestMapping("/editPage")
    public String toEdit(Long id, Model model){//编辑这里需要查询数据进行回显
        ServiceItem serviceItem = serviceItemService.get(id);
        model.addAttribute("serviceItem",serviceItem);
        return prefix+"edit";
    }
    //toauditPage页面
    @RequestMapping("/auditPage")
    public String auditPage(Long id,Model model){
        //根据页面信息，保证查询要回显的所有数据：服务单项+角色信息+自定义审核流程定义
        ServiceItem serviceItem = serviceItemService.get(id);
        model.addAttribute("serviceItem",serviceItem);
        List<User> shopOwner = userService.queryByRoleKey("shopOwner");
        model.addAttribute("directors",shopOwner);
        List<User> financial = userService.queryByRoleKey("financial");
        model.addAttribute("finances",financial);
        BpmnInfo bpmnInfo=bpmnInfoService.queryByType("car_package");
        model.addAttribute("bpmnInfo",bpmnInfo);
        return prefix+"/audit";
    }

    //分页+过滤查询
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<ServiceItem> query(ServiceItemQuery qo){
        TablePageInfo<ServiceItem> tablePageInfo = serviceItemService.query(qo);
        return tablePageInfo;
    }
    //添加
    @RequestMapping("/add")
    @ResponseBody
    public AjaxResult add(ServiceItem serviceItem){
        //补充数据
        //如果是套餐则审核状态：需要审核；否则初始化
        if(ServiceItem.CARPACKAGE_YES.equals(serviceItem.getCarPackage())){
            serviceItem.setAuditStatus(ServiceItem.AUDITSTATUS_AUDITING);//1审核状态
        }else {
            serviceItem.setAuditStatus(ServiceItem.AUDITSTATUS_INIT);//0初始化状态
        }
        serviceItem.setSaleStatus(ServiceItem.SALESTATUS_OFF);//默认不上架状态

        serviceItem.setCreateTime(new Date());
        serviceItem.setVersion(0L);
        serviceItemService.save(serviceItem);
        return new AjaxResult().success("添加成功！");
    }
    //修改
    @RequestMapping("/edit")
    @ResponseBody
    public AjaxResult edit(ServiceItem serviceItem){
        //这里修改sql语句来防止其它字段被修改
        serviceItemService.update(serviceItem);
        return new AjaxResult().success("修改成功！");
    }
    //上架
    @RequestMapping("/saleOn")
    @ResponseBody
    public AjaxResult saleOn(Long id){
        serviceItemService.saleOn(id);
        return new AjaxResult().success("上架成功！");
    }
    //下架
    @RequestMapping("/saleOff")
    @ResponseBody
    public AjaxResult saleOff(Long id){
        serviceItemService.saleOff(id);
        return new AjaxResult().success();
    }

    //审核：简单理解为 启动流程+添加流程审核记录表
    @RequestMapping("/startAudit")
    @ResponseBody
    public AjaxResult startAudit(Long id,Long bpmnInfoId,Long director,Long finance,String info){
        serviceItemService.startAudit(id,bpmnInfoId,director,finance,info);
        return new AjaxResult().success("开始审核！");
    }

}
